<?php

namespace App\Models;

use App\Helper;
use App\Models\Player;
use App\Models\Server;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Account extends Authenticatable
{
    protected $connection = 'mysql_auth';
    protected $table = 'account';
    public $timestamps = false;

//    protected $appends=['ban'];

    public function getBan()
    {
        $count=AccountBan::where('active',1)->where('id',$this->id)->count();
        return $count;
    }

    public function isGM()
    {
        $count=AccountAccess::where('id',$this->id)->count();
        return $count;
    }

//    use WowModel;

    public static function getAccount($server){
        $acc_id=json_decode( session('player'))->id;
        Helper::setDataBaseConf('mysql_auth',$server,'acore_auth');
        $acc=Account::find($acc_id);
        $play=Player::getPlayer($server,$acc->username);
        $acc->money=$play?$play->money:0;
        return $acc;
    }
    public function getPlayer($sid){
        return Player::where('server_id',$sid)->where('player_id',$this->id)->first();
    }



    public static function generateSalt()
    {
        return random_bytes(32);
    }
    public static function getVerifier($username, $password, $salt)
    {
        // algorithm constants
        $g = gmp_init(7);
        $N = gmp_init('894B645E89E1535BBDAD5B8B290650530801B18EBFBF5E8FAB3C82872A3E9BB7', 16);

        // calculate first hash
        $h1 = sha1(strtoupper($username . ':' . $password), true);

        // calculate second hash
        $h2 = sha1($salt . $h1, true);

        // convert to integer (little-endian)
        $h2 = gmp_import($h2, 1, GMP_LSW_FIRST);

        // g^h2 mod N
        $verifier = gmp_powm($g, $h2, $N);

        // convert back to a byte array (little-endian)
        $verifier = gmp_export($verifier, 1, GMP_LSW_FIRST);

        // pad to 32 bytes, remember that zeros go on the end in little-endian!
        $verifier = str_pad($verifier, 32, chr(0), STR_PAD_RIGHT);

        return $verifier;
    }

}
